﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Windows.Forms;

namespace StudentApartMentManage
{
    public partial class AutomaticAllocation : Form
    {
        string gylh, gyh;

        public AutomaticAllocation()
        {
            InitializeComponent();
        }
        void show()
        {
            string sql = string.Format("select * from 学生信息表 where 是否住宿=0");
            dataGridView1.DataSource = sqlHelper.GetTable(sql);

        }
        string GetRstr(string sql,int i)
        {
            string[] str = new string[2];
            string connectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand(sql, con);
            con.Open();
            SqlDataReader r = cmd.ExecuteReader();
            if (r.Read())
            {
                str[i] = r.GetString(i);
            }
            r.Close();
            con.Close();
            return str[i];
        }
        private void AutomaticAllocation_Load(object sender, EventArgs e)
        {
            show();
            comboBox1.ValueMember = "学号";
            comboBox1.DisplayMember = "学院";
            comboBox1.DataSource = sqlHelper.GetTable("select distinct 学院 from 学生信息表");
            comboBox2.ValueMember = "学号";
            comboBox2.DisplayMember = "专业";
            comboBox2.DataSource = sqlHelper.GetTable("select distinct 专业 from 学生信息表");
            comboBox3.ValueMember = "学号";
            comboBox3.DisplayMember = "班级";
            comboBox3.DataSource = sqlHelper.GetTable("select distinct 班级 from 学生信息表");
            dataGridView2.DataSource = DataShow.show("select * from 公寓分配表");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sql = string.Format("select * from 学生信息表 where 学院 like '{0}%' and 专业 like '{1}%' and 班级 like '{2}%' and 学号 like '{3}%' and 民族 like '{4}%' and 性别 = '{5}' and 是否住宿=0", comboBox1.Text, comboBox2.Text, comboBox3.Text, textBox1.Text, textBox2.Text, radioButton1.Checked ? "男" : "女");
            dataGridView1.DataSource = sqlHelper.GetTable(sql);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你真的要重置查找信息吗?", "警告", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
            {
                comboBox1.Text = "";
                comboBox2.Text = "";
                comboBox3.Text = "";
                textBox1.Clear();
                textBox2.Clear();
                radioButton1.Checked = true;
                radioButton2.Checked = false;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你真的要为该学生自动分配宿舍吗?", "警告", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
            {
                if (dataGridView1.SelectedCells[2].Value.ToString() == "男")
                {
                    gylh=GetRstr("select  top 1 公寓信息表.公寓楼号,公寓号 from 公寓信息表 join 公寓楼房信息表 on 公寓信息表.公寓楼号=公寓楼房信息表.公寓楼号 where 公寓类别=1 and 是否住满=0",0);
                    gyh= GetRstr("select  top 1 公寓信息表.公寓楼号,公寓号 from 公寓信息表 join 公寓楼房信息表 on 公寓信息表.公寓楼号=公寓楼房信息表.公寓楼号 where 公寓类别=1 and 是否住满=0", 1);
                    MessageBox.Show(gylh);
                    MessageBox.Show(gyh);
                    string zdinsert = string.Format("exec zdinsert '{0}','{1}', '{2}'", gylh, gyh, dataGridView1.SelectedCells[0].Value.ToString());
                    int i = sqlHelper.Execute(zdinsert);
                    if (i != -1)
                        MessageBox.Show("自动分配成功!");
                    else
                        MessageBox.Show("自动分配失败!");
                    show();
                    dataGridView2.DataSource = DataShow.show("select * from 公寓分配表");

                }
                else
                {

                    gylh = GetRstr("select  top 1 公寓信息表.公寓楼号,公寓号 from 公寓信息表 join 公寓楼房信息表 on 公寓信息表.公寓楼号=公寓楼房信息表.公寓楼号 where 公寓类别=0 and 是否住满=0", 0);
                    gyh = GetRstr("select  top 1 公寓信息表.公寓楼号,公寓号 from 公寓信息表 join 公寓楼房信息表 on 公寓信息表.公寓楼号=公寓楼房信息表.公寓楼号 where 公寓类别=0 and 是否住满=0", 1);
                    string zdinsert = string.Format("exec zdinsert '{0}','{1}', '{2}'", gylh, gyh, dataGridView1.SelectedCells[0].Value.ToString());
                    int i = sqlHelper.Execute(zdinsert);
                    if (i != -1)
                        MessageBox.Show("自动分配成功!");
                    else
                        MessageBox.Show("自动分配失败!");
                    show();
                    dataGridView2.DataSource = DataShow.show("select * from 公寓分配表");
                }
            }
        }
    }
}
